package com.gzu.ltms.holder;

import java.util.Objects;

public class LoginUserContextHolder {

    // 初始化一个 ThreadLocal 变量
    private static final ThreadLocal<Object> LOGIN_USER_CONTEXT_THREAD_LOCAL
            = new ThreadLocal<>();

    /**
     * 设置用户 ID
     *
     * @param value
     */
    public static void setUserId(Object value) {
        LOGIN_USER_CONTEXT_THREAD_LOCAL.set(value);
    }

    /**
     * 获取用户 ID
     *
     * @return
     */
    public static Long getUserId() {
        Object o = LOGIN_USER_CONTEXT_THREAD_LOCAL.get();
        if (Objects.isNull(o)) {
            return null;
        }
        return Long.valueOf(o.toString());
    }

    /**
     * 删除 ThreadLocal
     */
    public static void remove() {
        LOGIN_USER_CONTEXT_THREAD_LOCAL.remove();
    }

}

